package com.jpro.module.system.controller.admin.usergroup;

import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.excel.core.util.ExcelUtils;
import com.jpro.module.system.controller.admin.usergroup.vo.*;
import com.jpro.module.system.convert.usergroup.UserGroupConvert;
import com.jpro.module.system.dal.dataobject.usergroup.UserGroupDO;
import com.jpro.module.system.service.usergroup.UserGroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.jpro.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 用户和会员组关联")
@RestController
@RequestMapping("/cms/user-group")
@Validated
public class UserGroupController {

    @Resource
    private UserGroupService userGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建用户和会员组关联")
    @PreAuthorize("@ss.hasPermission('cms:user-group:create')")
    public CommonResult<Long> createUserGroup(@Valid @RequestBody UserGroupCreateReqVO createReqVO) {
        return success(userGroupService.createUserGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户和会员组关联")
    @PreAuthorize("@ss.hasPermission('cms:user-group:update')")
    public CommonResult<Boolean> updateUserGroup(@Valid @RequestBody UserGroupUpdateReqVO updateReqVO) {
        userGroupService.updateUserGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户和会员组关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:user-group:delete')")
    public CommonResult<Boolean> deleteUserGroup(@RequestParam("id") Long id) {
        userGroupService.deleteUserGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户和会员组关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:user-group:query')")
    public CommonResult<UserGroupRespVO> getUserGroup(@RequestParam("id") Long id) {
        UserGroupDO userGroup = userGroupService.getUserGroup(id);
        return success(UserGroupConvert.INSTANCE.convert(userGroup));
    }

    @GetMapping("/list")
    @Operation(summary = "获得用户和会员组关联列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('cms:user-group:query')")
    public CommonResult<List<UserGroupRespVO>> getUserGroupList(@RequestParam("ids") Collection<Long> ids) {
        List<UserGroupDO> list = userGroupService.getUserGroupList(ids);
        return success(UserGroupConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户和会员组关联分页")
    @PreAuthorize("@ss.hasPermission('cms:user-group:query')")
    public CommonResult<PageResult<UserGroupRespVO>> getUserGroupPage(@Valid UserGroupPageReqVO pageVO) {
        PageResult<UserGroupDO> pageResult = userGroupService.getUserGroupPage(pageVO);
        return success(UserGroupConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户和会员组关联 Excel")
    @PreAuthorize("@ss.hasPermission('cms:user-group:export')")

    public void exportUserGroupExcel(@Valid UserGroupExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserGroupDO> list = userGroupService.getUserGroupList(exportReqVO);
        // 导出 Excel
        List<UserGroupExcelVO> datas = UserGroupConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户和会员组关联.xls", "数据", UserGroupExcelVO.class, datas);
    }

}
